// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "GameFeatureAction_WorldActionBase.h"
#include "UObject/SoftObjectPtr.h"
#include "GameFeatureAction_AddInputBinding.generated.h"

class AActor;
class UInputMappingContext;
class UPlayer;
class APlayerController;
struct FComponentRequestHandle;
class ULyraInputConfig;

/**
 * 将InputMappingContext添加到本地玩家的EnhancedInput系统
 * 期望本地玩家已设置为使用EnhancedInput系统
 */
UCLASS(MinimalAPI, meta = (DisplayName = "Add Input Binds"))
class UGameFeatureAction_AddInputBinding final : public UGameFeatureAction_WorldActionBase
{
	GENERATED_BODY()

public:
	//~ 开始 UGameFeatureAction 接口
	virtual void OnGameFeatureActivating(FGameFeatureActivatingContext& Context) override; // 游戏特性激活时调用
	virtual void OnGameFeatureDeactivating(FGameFeatureDeactivatingContext& Context) override; // 游戏特性停用时调用
	//~ 结束 UGameFeatureAction 接口

	//~ 开始 UObject 接口
#if WITH_EDITOR
	virtual EDataValidationResult IsDataValid(class FDataValidationContext& Context) const override; // 数据验证
#endif
	//~ 结束 UObject 接口

	UPROPERTY(EditAnywhere, Category="Input", meta=(AssetBundles="Client,Server"))
	TArray<TSoftObjectPtr<const ULyraInputConfig>> InputConfigs; // 输入配置列表

private:
	/**
	 * 每个上下文的数据结构
	 */
	struct FPerContextData
	{
		TArray<TSharedPtr<FComponentRequestHandle>> ExtensionRequestHandles; // 扩展请求句柄数组
		TArray<TWeakObjectPtr<APawn>> PawnsAddedTo; // 已添加到的Pawn数组
	};

	TMap<FGameFeatureStateChangeContext, FPerContextData> ContextData; // 上下文数据映射

	//~ 开始 UGameFeatureAction_WorldActionBase 接口
	virtual void AddToWorld(const FWorldContext& WorldContext, const FGameFeatureStateChangeContext& ChangeContext) override; // 添加到世界
	//~ 结束 UGameFeatureAction_WorldActionBase 接口

	void Reset(FPerContextData& ActiveData); // 重置数据
	void HandlePawnExtension(AActor* Actor, FName EventName, FGameFeatureStateChangeContext ChangeContext); // 处理Pawn扩展
	void AddInputMappingForPlayer(APawn* Pawn, FPerContextData& ActiveData); // 为玩家添加输入映射
	void RemoveInputMapping(APawn* Pawn, FPerContextData& ActiveData); // 移除输入映射
};